
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>多Agent协作演示</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        .container {
            width: 90%;
            max-width: 1200px;
            height: 90vh;
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            display: flex;
            flex-direction: column;
            overflow: hidden;
        }
        
        .header {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
            padding: 20px;
            text-align: center;
        }
        
        .header h1 {
            font-size: 24px;
            margin-bottom: 5px;
        }
        
        .header p {
            opacity: 0.9;
            font-size: 14px;
        }
        
        .chat-container {
            flex: 1;
            display: flex;
            flex-direction: column;
            padding: 20px;
        }
        
        .messages {
            flex: 1;
            overflow-y: auto;
            border: 1px solid #e0e0e0;
            border-radius: 10px;
            padding: 15px;
            margin-bottom: 20px;
            background: #fafafa;
        }
        
        .message {
            margin-bottom: 15px;
            padding: 12px;
            border-radius: 10px;
            animation: fadeIn 0.3s ease-in;
        }
        
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        .message.user_input {
            background: #e3f2fd;
            border-left: 4px solid #2196f3;
        }
        
        .message.agent_thinking {
            background: #fff3e0;
            border-left: 4px solid #ff9800;
        }
        
        .message.agent_action {
            background: #e8f5e8;
            border-left: 4px solid #4caf50;
        }
        
        .message.agent_message {
            background: #f3e5f5;
            border-left: 4px solid #9c27b0;
        }
        
        .message.final_result {
            background: #e1f5fe;
            border-left: 4px solid #00bcd4;
            font-weight: bold;
        }
        
        .message.system_status {
            background: #fce4ec;
            border-left: 4px solid #e91e63;
        }
        
        .message-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 8px;
            font-size: 12px;
            color: #666;
        }
        
        .message-sender {
            font-weight: bold;
            color: #333;
        }
        
        .message-time {
            color: #999;
        }
        
        .message-content {
            font-size: 14px;
            line-height: 1.5;
            color: #333;
        }
        
        .message-data {
            margin-top: 10px;
            padding: 10px;
            background: rgba(0,0,0,0.05);
            border-radius: 5px;
            font-size: 12px;
            font-family: monospace;
        }
        
        .input-container {
            display: flex;
            gap: 10px;
        }
        
        .input-field {
            flex: 1;
            padding: 12px;
            border: 2px solid #e0e0e0;
            border-radius: 25px;
            font-size: 14px;
            outline: none;
            transition: border-color 0.3s;
        }
        
        .input-field:focus {
            border-color: #4facfe;
        }
        
        .send-button {
            padding: 12px 24px;
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
            border: none;
            border-radius: 25px;
            cursor: pointer;
            font-size: 14px;
            font-weight: bold;
            transition: transform 0.2s;
        }
        
        .send-button:hover {
            transform: translateY(-2px);
        }
        
        .send-button:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }
        
        .status-indicator {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 10px 15px;
            border-radius: 20px;
            color: white;
            font-size: 12px;
            font-weight: bold;
        }
        
        .status-connected {
            background: #4caf50;
        }
        
        .status-disconnected {
            background: #f44336;
        }
        
        .examples {
            margin-top: 10px;
            font-size: 12px;
            color: #666;
        }
        
        .example-button {
            display: inline-block;
            margin: 2px;
            padding: 4px 8px;
            background: #f0f0f0;
            border: 1px solid #ddd;
            border-radius: 12px;
            cursor: pointer;
            transition: background 0.2s;
        }
        
        .example-button:hover {
            background: #e0e0e0;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🤖 多Agent协作演示系统</h1>
            <p>体验智能Agent间的实时协作、协商和决策过程</p>
        </div>
        
        <div class="chat-container">
            <div class="messages" id="messages"></div>
            
            <div class="input-container">
                <input type="text" class="input-field" id="messageInput" 
                       placeholder="请输入您的需求，例如：安排明天的项目会议" 
                       onkeypress="handleKeyPress(event)">
                <button class="send-button" id="sendButton" onclick="sendMessage()">发送</button>
            </div>
            
            <div class="examples">
                <strong>示例：</strong>
                <span class="example-button" onclick="setExample('安排明天的项目会议')">安排项目会议</span>
                <span class="example-button" onclick="setExample('预订下周的培训室')">预订培训室</span>
                <span class="example-button" onclick="setExample('安排客户演示会议')">客户演示</span>
                <span class="example-button" onclick="setExample('组织团队建设活动')">团队建设</span>
            </div>
        </div>
    </div>
    
    <div class="status-indicator" id="statusIndicator">连接中...</div>
    
    <script>
        let ws = null;
        let isConnected = false;
        
        function connect() {
            ws = new WebSocket('ws://localhost:8765');
            
            ws.onopen = function(event) {
                isConnected = true;
                updateStatus('已连接', 'connected');
                console.log('WebSocket连接已建立');
            };
            
            ws.onmessage = function(event) {
                const message = JSON.parse(event.data);
                displayMessage(message);
            };
            
            ws.onclose = function(event) {
                isConnected = false;
                updateStatus('连接断开', 'disconnected');
                console.log('WebSocket连接已关闭');
                // 尝试重连
                setTimeout(connect, 3000);
            };
            
            ws.onerror = function(error) {
                console.error('WebSocket错误:', error);
                updateStatus('连接错误', 'disconnected');
            };
        }
        
        function updateStatus(text, status) {
            const indicator = document.getElementById('statusIndicator');
            indicator.textContent = text;
            indicator.className = 'status-indicator status-' + status;
        }
        
        function displayMessage(message) {
            const messagesDiv = document.getElementById('messages');
            const messageDiv = document.createElement('div');
            messageDiv.className = 'message ' + message.type;
            
            const headerDiv = document.createElement('div');
            headerDiv.className = 'message-header';
            headerDiv.innerHTML = `
                <span class="message-sender">${message.sender}</span>
                <span class="message-time">${message.timestamp}</span>
            `;
            
            const contentDiv = document.createElement('div');
            contentDiv.className = 'message-content';
            contentDiv.textContent = message.content;
            
            messageDiv.appendChild(headerDiv);
            messageDiv.appendChild(contentDiv);
            
            // 如果有数据，显示数据
            if (message.data && Object.keys(message.data).length > 0) {
                const dataDiv = document.createElement('div');
                dataDiv.className = 'message-data';
                dataDiv.textContent = JSON.stringify(message.data, null, 2);
                messageDiv.appendChild(dataDiv);
            }
            
            messagesDiv.appendChild(messageDiv);
            messagesDiv.scrollTop = messagesDiv.scrollHeight;
        }
        
        function sendMessage() {
            const input = document.getElementById('messageInput');
            const message = input.value.trim();
            
            if (message && isConnected) {
                const data = {
                    type: 'user_input',
                    content: message
                };
                
                ws.send(JSON.stringify(data));
                input.value = '';
                
                // 禁用发送按钮一段时间
                const sendButton = document.getElementById('sendButton');
                sendButton.disabled = true;
                setTimeout(() => {
                    sendButton.disabled = false;
                }, 2000);
            }
        }
        
        function handleKeyPress(event) {
            if (event.key === 'Enter') {
                sendMessage();
            }
        }
        
        function setExample(text) {
            document.getElementById('messageInput').value = text;
        }
        
        // 启动连接
        connect();
    </script>
</body>
</html>
